package com.lindroy.androidplayer.ui.login

import androidx.lifecycle.MutableLiveData
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.viewmodel.BaseViewModel
import com.lindroy.androidplayer.extensions.request
import com.lindroy.androidplayer.logic.model.LoginResp
import com.lindroy.androidplayer.logic.network2.state.ResultState
import com.lindroy.androidplayer.logic.network2.HttpRequestManager
import com.lindroy.androidplayer.logic.network2.apiService
import com.lindroy.lib_utils.extensions.getResString

/**
 * @author Lin
 * @date 2021/4/9
 * @function
 */
class LoginViewModel : BaseViewModel() {

//    fun register(name: String, pwd: String, repwd: String) = Repository.register(name, pwd, repwd)

    var loginResult = MutableLiveData<ResultState<LoginResp>>()

    var logoutResult = MutableLiveData<ResultState<Any?>>()

    fun login(name: String, pwd: String) {
        request(
            { apiService.login(name, pwd) },
            loginResult,
            true,
            getResString(R.string.logging)
        )
    }

    fun registerAndLogin(name: String, pwd: String) {
        request(
            { HttpRequestManager.register(name, pwd) },
            loginResult,
            true,
            getResString(R.string.registing)
        )
    }

    fun logout() {
        request({ apiService.logout() }, logoutResult, true, getResString(R.string.logoutting))
    }


}